package com.itheima.advantagesup.service.impl;

import com.itheima.advantagesup.dto.SyssMenuDTO;
import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.mapper.SysMenuMapper;
import com.itheima.advantagesup.service.SysMenuService;
import com.itheima.advantagesup.utils.MenuUtils;
import com.itheima.advantagesup.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceLmpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    //查询菜单
    @Override
    public List<MenuVO> findAllMenu() {
        List<MenuVO> menuVOList = new ArrayList<>();
        List<SysMenu> findAllMenu = sysMenuMapper.findAllMenu();
        for (SysMenu allMenu : findAllMenu) {
            MenuVO menuVO = new MenuVO();
            menuVO.setId(allMenu.getId());
            menuVO.setPid(allMenu.getPid());
            menuVO.setPath(allMenu.getPath());
            menuVO.setComponent(allMenu.getComponent());
            menuVO.setTitle(allMenu.getTitle());
            menuVO.setIcon(allMenu.getIcon());
            menuVO.setIs_show(allMenu.getIsShow());
            menuVO.setIs_cache(allMenu.getIsCache());
            menuVO.setIs_link(allMenu.getIsLink());
            menuVO.setRedirect(allMenu.getRedirect());
            menuVO.setSort(allMenu.getSort());
            menuVO.setMark(allMenu.getMark());
            menuVO.setCreateDate(allMenu.getCreateDate());
            menuVO.setUpdateDate(allMenu.getUpdateDate());
            menuVO.setDel_flag(allMenu.getDelFlag());
            menuVO.setType(allMenu.getType());
            menuVOList.add(menuVO);
        }
        List<MenuVO> menvo = MenuUtils.getMenuVOList(menuVOList);
        return menvo;
    }
//添加菜单
    @Override
    public void AddMenu(SyssMenuDTO syssMenuDTO) {
        SysMenu sysMenu=new SysMenu();
        sysMenu.setComponent(syssMenuDTO.getComponent());
        sysMenu.setIcon(syssMenuDTO.getIcon());
        sysMenu.setIsCache(syssMenuDTO.getIs_cache());
        sysMenu.setIsLink(syssMenuDTO.getIs_link());
        sysMenu.setIsShow(syssMenuDTO.getIs_show());
        sysMenu.setMark(syssMenuDTO.getMark());
        sysMenu.setPath(syssMenuDTO.getPath());
        sysMenu.setPid(syssMenuDTO.getPid());
        sysMenu.setRedirect(syssMenuDTO.getRedirect());
        sysMenu.setSort(syssMenuDTO.getSort());
        sysMenu.setTitle(syssMenuDTO.getTitle());
        sysMenu.setType(syssMenuDTO.getType());
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());


        sysMenuMapper.AddMenu(sysMenu);
    }
//根据id删除菜单
    @Override
    public void DeleteId(Integer id) {
        sysMenuMapper.DeleteId(id);
    }
//根据id查询菜单
    @Override
    public MenuVO findById(Integer id) {
        SysMenu menulist=sysMenuMapper.findById(id);
            MenuVO menuVO=new MenuVO();
            menuVO.setId(menulist.getId());
            menuVO.setPid(menulist.getPid());
            menuVO.setPath(menulist.getPath());
            menuVO.setComponent(menulist.getComponent());
            menuVO.setTitle(menulist.getTitle());
            menuVO.setIcon(menulist.getIcon());
            menuVO.setIs_show(menulist.getIsShow());
            menuVO.setIs_cache(menulist.getIsCache());
            menuVO.setIs_link(menulist.getIsLink());
            menuVO.setRedirect(menulist.getRedirect());
            menuVO.setSort(menulist.getSort());
            menuVO.setMark(menulist.getMark());
            menuVO.setCreateDate(menulist.getCreateDate());
            menuVO.setUpdateDate(menulist.getUpdateDate());
            menuVO.setDel_flag(menulist.getDelFlag());
            menuVO.setType(menulist.getType());
        return menuVO;
    }

    @Override
    public void updateById(SyssMenuDTO syssMenuDTO) {
        SysMenu sysMenu=new SysMenu();
        sysMenu.setComponent(syssMenuDTO.getComponent());
        sysMenu.setIcon(syssMenuDTO.getIcon());
        sysMenu.setIsCache(syssMenuDTO.getIs_cache());
        sysMenu.setIsLink(syssMenuDTO.getIs_link());
        sysMenu.setIsShow(syssMenuDTO.getIs_show());
        sysMenu.setMark(syssMenuDTO.getMark());
        sysMenu.setPath(syssMenuDTO.getPath());
        sysMenu.setPid(syssMenuDTO.getPid());
        sysMenu.setRedirect(syssMenuDTO.getRedirect());
        sysMenu.setSort(syssMenuDTO.getSort());
        sysMenu.setTitle(syssMenuDTO.getTitle());
        sysMenu.setType(syssMenuDTO.getType());
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setId(syssMenuDTO.getId());
        sysMenuMapper.updateById(sysMenu);
    }
}
